//
//  SettingViewController.swift
//  RHello
//
//  Created by 吴智民 on 2020/5/21.
//  Copyright © 2020 Lanchc. All rights reserved.
//

import UIKit

/**代理设置*/
protocol ConfigureDelegate: AnyObject {
    func setup(hostip: String, port: String)
}


class SettingViewController: UIViewController {
    
    weak var delegate: ConfigureDelegate?
    
    /**IP地址*/
    @IBOutlet weak var hostip: UITextField!
    /**端口*/
    @IBOutlet weak var ported: UITextField!
    /**日志*/
    @IBOutlet weak var logger: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        hostip.text = SkManager.default.askCurrentLinkIp()
        ported.text = "2001"
        
        // 过滤弹窗
        DispatchQueue.global().async {
            SkQueue.default.askDemo()
        }
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        SkManager.default.tx = logger
        SkQueue.default.tx = logger
    }
    
    
    
    /// 连接
    @IBAction func connectTap(_ sender: UIButton) {
        self.view.endEditing(true)
        
        SkManager.default.clientSocket.disconnect()
        
        SkManager.default.loadURL()
        
        SkManager.default.hsip = hostip.text ?? ""
        SkManager.default.prot = ported.text ?? ""
        
        
        do {
            guard let xip = hostip.text else {
                SkManager.default.showMessage("请输入连接地址")
                return
            }
            
            guard let xport = ported.text else {
                SkManager.default.showMessage("请输入连接端口")
                return
            }
            
            try SkManager.default.clientSocket.connect(toHost: xip, onPort: UInt16(xport)!, withTimeout: -1)
            
            /// 保存ip
            SkManager.default.writeInLinkIp(hostURL: xip)
            
        } catch {
            SkManager.default.showMessage("链接失败")
        }
    }
    
    
    /// 断开
    @IBAction func disconnectTap(_ sender: UIButton) {
        
        self.view.endEditing(true)
        
        SkManager.default.clientSocket.disconnect()
        SkManager.default.showMessage("断开链接")
        
        hostip.text = SkManager.default.askCurrentLinkIp()
    }
    
    @IBAction func onTouchScan(_ sender: UIButton) {
        self.view.endEditing(true)
        SkManager.default.writeInLinkIp(hostURL: "")
        
        SkManager.default.showMessage("扫描ip")
        SkQueue.default.scanningDevice(bIndex: 1, eIndex: 50) {
            SkQueue.default.scanningDevice(bIndex: 51, eIndex: 100) {
                SkQueue.default.scanningDevice(bIndex: 101, eIndex: 150) {
                    SkQueue.default.scanningDevice(bIndex: 151, eIndex: 200) {
                        SkQueue.default.scanningDevice(bIndex: 201, eIndex: 250) {
                            SkQueue.default.scanningDevice(bIndex: 251, eIndex: 255) {
                                print("全部解析ip一圈")
                            }
                        }
                    }
                }
            }
        }
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
